import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class SelfSort {
    public String customSortString(String S, String T) {
//        if(S.equals("")|| T.equals("")){
//            return "";
//        }
//        //模式串
//        char[] charsS = S.toCharArray();
//        //与charS数组对应，记录每个字符被匹配的次数
//        int[] count = new int[charsS.length];
//        //这个数组与charS数组对应，记录哪些字符被匹配到
//        int[] sIndex = new int[charsS.length];
//        //待匹配的串
//        char[] charsT = T.toCharArray();
//        //与charT数组对应，记录哪些字符被匹配
//        int[] tIndex = new int[charsT.length];
//        //用于连接新字符串
//        StringBuffer sbf = new StringBuffer();
//        //外层循环是待匹配串长度
//        for(int i=0;i<charsT.length;i++){
//            //内层循环是模式串长度
//            for (int j=0;j<charsS.length;j++){
//                //哪些字符被匹配到sIndex,tIndex数组对应的值由0变为1。count累加1
//                if(charsT[i]==charsS[j]){
//                    sIndex[j]=1;
//                    count[j] +=1;
//                    tIndex[i]=1;
//                }
//            }
//        }
//        //遍历sIndex，值为1 charsS对应的就拼接，count为几就代表当前该字符要拼接的次数
//        for (int i=0;i<sIndex.length;i++){
//            if(sIndex[i]==1&&count[i]==1){
//                sbf.append(charsS[i]);
//            }else if(sIndex[i]==1&&count[i]>1){
//                for (int k=0;k<count[i];k++){
//                    sbf.append(charsS[i]);
//                }
//            }
//        }
//        //T字符串中有哪些字符没有匹配到，就拼接在后面
//        for (int j = 0; j < tIndex.length; j++) {
//            if(tIndex[j]==0){
//                sbf.append(charsT[j]);
//            }
//        }
//
//        return sbf.toString();
        // count[char] = the number of occurrences of 'char' in T.
        // This is offset so that count[0] = occurrences of 'a', etc.
        // 'count' represents the current state of characters
        // (with multiplicity) we need to write to our answer.
        int[] count = new int[26];
        for (char c: T.toCharArray()){
            count[c - 'a']++;
        }
        // ans will be our final answer.  We use StringBuilder to join
        // the answer so that we more efficiently calculate a
        // concatenation of strings.
        StringBuilder ans = new StringBuilder();

        // Write all characters that occur in S, in the order of S.
        for (char c: S.toCharArray()) {
            for (int i = 0; i < count[c - 'a']; ++i){
                ans.append(c);
            }
            // Setting count[char] to zero to denote that we do
            // not need to write 'char' into our answer anymore.
            count[c - 'a'] = 0;
        }

        // Write all remaining characters that don't occur in S.
        // That information is specified by 'count'.
        for (char c = 'a'; c <= 'z'; ++c){
            for (int i = 0; i < count[c - 'a']; ++i){
                ans.append(c);
            }

        }
        return ans.toString();

    }
//"cbafg"
//        "abcd"
    public static void main(String[] args) {
        String s = new SelfSort().customSortString("cbafg", "abcd");
        System.out.println(s);
    }
}
